<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8">
    <title>班级列表</title>
    <link href='<c:url context="${pageContext.request.contextPath}" value="/easyui/themes/default/easyui.css"/>'
          rel="stylesheet" type="text/css"/>
    <link href='<c:url context="${pageContext.request.contextPath}" value="/easyui/themes/icon.css"/>' rel="stylesheet"
          type="text/css"/>
    <link href='<c:url context="${pageContext.request.contextPath}" value="/easyui/css/demo.css"/>' rel="stylesheet"
          type="text/css"/>
    <script type="text/javascript"
            src='<c:url value="/easyui/jquery.min.js" context="${pageContext.request.contextPath}"/>'></script>
    <script type="text/javascript"
            src='<c:url value="/easyui/jquery.easyui.min.js" context="${pageContext.request.contextPath}"/>'></script>
    <script type="text/javascript"
            src='<c:url value="/easyui/js/validateExtends.js" context="${pageContext.request.contextPath}"/>'></script>
    <script type="text/javascript"
            src='<c:url value="/easyui/js/easyui-lang-zh_CN.js" context="${pageContext.request.contextPath}"/>'></script>
    <script type="text/javascript">
        $(function () {
            //datagrid初始化
            $('#dataList').datagrid({
                title: '班级列表',
                iconCls: 'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "get",
                url: "/student/clazz?action=data",
                idField: 'cid',
                singleSelect: true,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                pageSize: 2,//默认每页显示两条数据
                pageList: [2, 4, 6, 8, 10],
                sortName: 'cid',
                sortOrder: 'asc',
                remoteSort: false,
                columns: [[
                    {field: 'chk', checkbox: true, width: 50},
                    {field: 'cid', title: '班级编号', width: 80, sortable: true},
                    {field: 'clazzName', title: '班级名称', width: 200},
                    {
                        field: 'grade', title: '所属年级', width: 100,
                        formatter: function (value, row, index) {
                            if (row.grade) {
                                return row.grade.gradeName;
                            } else {
                                return value;
                            }
                        }
                    },
                ]],
                toolbar: "#toolbar"
            });
            //设置分页控件
            var p = $('#dataList').datagrid('getPager');
            $(p).pagination({
                // pageSize: 10,//每页显示的记录条数，默认为10
                // pageList: [10,20,30,50,100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
            //设置工具类按钮
            $("#add").click(function () {
                $("#addDialog").dialog("open");
            });
            //删除
            $("#delete").click(function () {
                var selectRow = $("#dataList").datagrid("getSelected");
                if (selectRow == null) {
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else {
                    var clazzid = selectRow.cid;
                    $.messager.confirm("消息提醒", "将删除与班级相关的所有数据(包括学生)，确认继续？", function (r) {
                        if (r) {
                            $.ajax({
                                type: "delete",
                                url: "/student/clazz?cid=" + clazzid,
                                success: function (msg) {
                                    if (msg.status == 200) {
                                        $.messager.alert("消息提醒", msg.msg, "success");
                                        //刷新表格
                                        $("#dataList").datagrid("reload");
                                    }else if ( msg.status == 500) {
                                        $.messager.alert("消息提醒", msg.msg, "error");
                                    }else {
                                        $.messager.alert("消息提醒", msg.msg, "warning");
                                        return;
                                    }
                                }
                            });
                        }
                    });
                }
            });

            //搜索时候的年级下拉框
            $("#gradeList").combobox({
                width: "150",
                height: "25",
                valueField: "gid",
                textField: "gradeName",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "get",
                url: "/student/grade?action=data_from_clazz",
                onChange: function (newValue, oldValue) {//当下拉框的值发生改变的时候，会触发该方法
                    //查找到数据显示的表格，并且给表格设置一个查询条件，查询的 key 就是 gid，value 则是当前下拉框的最新值
                    if (newValue == -1) {
                        //取消 gid 参数
                        $('#dataList').datagrid("options").queryParams = {};
                    } else {
                        //加上 gid 参数
                        $('#dataList').datagrid("options").queryParams = {gid: newValue};
                    }
                    //刷新表格，此时，刷新表格的时候，会带着最新的参数去刷新
                    $('#dataList').datagrid("reload");
                }
            });

            //所属年级下拉框
            $("#add_gradeList").combobox({
                valueField: "gid",
                textField: "gradeName",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "get",
                url: "/student/grade?action=data",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].gid);
                }
            });

            //设置添加班级窗口
            $("#addDialog").dialog({
                title: "添加班级",
                width: 500,
                height: 400,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text: '添加',
                        plain: true,
                        iconCls: 'icon-add',
                        handler: function () {
                            var validate = $("#addForm").form("validate");
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据!", "warning");
                                return;
                            } else {
                                var gradeid = $("#add_gradeList").combobox("getValue");
                                $.ajax({
                                    type: "post",
                                    url: "/student/clazz",
                                    data: $("#addForm").serialize(),
                                    success: function (msg) {

                                        if (msg.status == 200) {
                                            $.messager.alert("消息提醒", msg.msg, "info");
                                            //关闭窗口
                                            $("#addDialog").dialog("close");
                                            //清空原表格数据
                                            $("#add_name").textbox('setValue', "");

                                            //重新刷新页面数据
                                            $('#gradeList').combobox("setValue", gradeid);
                                            $('#dataList').datagrid("reload");

                                        } else {
                                            $.messager.alert("消息提醒", msg.msg, "warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-reload',
                        handler: function () {
                            $("#add_name").textbox('setValue', "");
                            //重新加载年级
                            $("#add_gradeList").combobox("reload");
                        }
                    },
                ]
            });

        });
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">
</table>

<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton"
                                                     data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>

    <div style="margin: 0 10px 0 10px">年级：<select id="gradeList" class="easyui-textbox" name="grade">
        <%--        <option value="全部">全部</option>--%>
    </select></div>
</div>

<!-- 添加窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8">
            <tr>
                <td>班级名称:</td>
                <td><input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text"
                           name="clazzName" validType="repeat_clazz['#add_gradeList']"
                           data-options="required:true, missingMessage:'不能为空'"/></td>
            </tr>
            <tr>
                <td>所属年级:</td>
                <td><select id="add_gradeList" style="width: 200px; height: 30px;" name="gid"></select></td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>
